package com.unicampmaps;

import java.util.Vector;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class SearchActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		AutoCompleteTextView txtView = (AutoCompleteTextView)findViewById(R.id.searchTextView);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
		
		for (String str : getAutocompletionList()) {
			adapter.add(str);
		}
		
		txtView.setAdapter(adapter);
	}

	public String[] getAutocompletionList(){
		Vector<String> vec = new Vector<String>();
		
		Cursor c = getContentResolver().query(VenueProvider.CONTENT_URI, 
				new String[]{VenueData.C_NAME},
				null,null, null);
		
		if(c!=null){
			c.moveToFirst();
			
			for (int i = 0; i < c.getCount(); i++) {
				vec.add(c.getString(c.getColumnIndexOrThrow(VenueData.C_NAME)));
				c.moveToNext();
			}
			
			c.close();
		}
		return (String[])vec.toArray(new String[vec.size()]);
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search, menu);
		return true;
	}

	
	public void search(View v){
		Intent searchIntent = new Intent(this, ListActivity.class);
		AutoCompleteTextView txtView = (AutoCompleteTextView)findViewById(R.id.searchTextView);
		String searchText = txtView.getText().toString().trim();
		
		if(searchText.length()>0){
			searchIntent.putExtra("search_text", searchText);
		}
		
		startActivity(searchIntent);
		this.finish();
	}
	
	public String[] getAutocompleteOptions(String str){
		
		return null;
	}
	
	public void cancelSearch(View v){
		finish();
	}
}
